package main

import (
	"crypto/sha512"
	"encoding/hex"
	"fmt"
	"reflect"
	"strconv"
	"strings"
)

/*
   function _getHashKey($posted, $salt_key){
       $hash_sequence = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";

       // make an array or split into array base on pipe sign.
       $hash_sequence_array = explode('|', $hash_sequence);
       $hash = null;

       // prepare a string based on hash sequence from the $params array.
       foreach ($hash_sequence_array as $value) {
           $hash .= isset($posted[$value]) ? $posted[$value] : '';
           $hash .= '|';
       }

       $hash .= $salt_key;
       #echo $hash;
       #echo " ";
       #echo strtolower(hash('sha512', $hash));
       // generate hash key using hash function(predefine) and return
       return strtolower(hash('sha512', $hash));
   }
 */

func GetHashKey(mp map[string]string) string{
	hash_sequence := "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10"
	hash_sequence_array := strings.Split(hash_sequence,"|")
	hash := ""
	for _, v := range hash_sequence_array {
		hash = hash + mp[v] + "|"
	}
	saltKey := "VQSF7VA8V5"
	hash = hash + saltKey
	fmt.Println("111", hash)
	hashObj := sha512.New()
	hashObj.Write([]byte(hash))
	hashString := hex.EncodeToString(hashObj.Sum(nil))
	fmt.Println("222", hashString)
	return hashString
}

func main()  {
	fmt.Println("________test__")
	/*
	"txnid" => "T3SAT0B5OL",
	"amount" => "100.0",
	"firstname" => "jitendra",
	"email" => "test@gmail.com",
	"phone" => "1231231235",
	"productinfo" => "Laptop",
	"surl" => "http://localhost:3000/response.php",
	"furl" => "http://localhost:3000/response.php",
	 */
	mp := map[string]string{
		"key" : "I3ZR1Y6LOC",
		"txnid" : "T3SAT0B5OL1",
		"amount" : "110",
		"firstname" : "jitendra",
		"email" : "test@gmail.com",
		"phone" : "1231231235",
		"productinfo" : "Laptop",
		"surl" : "http://localhost:3000/response.php",
		"furl" : "http://localhost:3000/response.php",
		"udf1" : "aaaa",
		"udf2" : "aaaa",
		"udf3" : "aaaa",
		"udf4" : "aaaa",
		"udf5" : "aaaa",
		"address1" : "aaaa",
		"address2" : "aaaa",
		"city" : "aaaa",
		"state" : "aaaa",
		"country" : "aaaa",
		"zipcode" : "123123",
	}
	str := GetHashKey(mp)
	fmt.Println("str ", str)


	mp2 := map[string]interface{}{
		"sss": true,
		"err":map[string]interface{}{
			"eee":22,
		},
	}
	if reflect.TypeOf(mp2["sss"]).String() != "bool" {
		fmt.Println(reflect.TypeOf(mp2["sss"]))
	}

	errCode := "msg3444444dfarqewrqewrqewr  sdafadfadfadf"
	if len(errCode) > 30 {
		errCode = errCode[0:2]
	}
	fmt.Println(errCode)

	mp3 := razorpayGetStringMap(mp2)

	fmt.Printf("mp3:%v \n", mp3)
	fmt.Println(reflect.TypeOf(mp3["err"]), mp3["err"], len(mp3["xx"]))


	fmt.Println(len("MAL_W_3659778_1599643277_1664"))

	amount := strconv.FormatFloat(float64(10.0), 'f', 1, 32)

	fmt.Println(amount)

	errCode2 := "errCode"
	errCode2 = errCode2[0:30]
	fmt.Println(errCode2)
}

func razorpayGetStringMap(mp map[string]interface{}) map[string]string {
	ret := make(map[string]string, len(mp))
	for k, v := range mp {
		ret[k] = fmt.Sprint(v)
	}
	return ret
}
